import java.io.IOException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.sql.SQLException;

import javax.swing.JTextArea;

import net.tinyos.packet.BuildSource;
import net.tinyos.packet.PacketSource;
import net.tinyos.util.PrintStreamMessenger;

/**
 * @author edhem.brakmic
 * 
 */

class ListenerThread extends Thread {

	private boolean threadDone = false;
	private boolean outputOnOff;
	private PacketSource reader;

	JTextArea textA;

	public void done() {
		threadDone = true;
	}

	public ListenerThread(String[] args, JTextArea textArea, boolean o) {
		outputOnOff = o;
		String source = null;
		if (args.length == 2 && args[0].equals("-comm")) {
			source = args[1];
		} else if (args.length > 0) {
			System.err
					.println("usage: java net.tinyos.tools.Listen [-comm PACKETSOURCE]");
			System.err
					.println("       (default packet source from MOTECOM environment variable)");
			System.exit(2);
		}
		if (source == null) {
			reader = BuildSource.makePacketSource();
		} else {
			reader = BuildSource.makePacketSource(source);
		}
		if (reader == null) {
			System.err
					.println("Invalid packet source (check your MOTECOM environment variable)");
			System.exit(2);
		}

		textA = textArea;

	}

	public void outputToogle(boolean o) {
		outputOnOff = o;

	}

	public void run() {
		DbConnector db = new DbConnector();
		System.out.print("listener starts ... \n");
		threadDone = false;
		try {
			reader.open(PrintStreamMessenger.err);

			while (threadDone == false) {
				byte[] packet = reader.readPacket();
				// Dump.printPacket(someOtherStream, packet);
				String bs = "";
				for (int i = 0; i < packet.length; i++) {

					if (packet[i] >= 0 && packet[i] < 16) {
						bs = bs+"0";
					}
					
					bs = bs
							+ Integer.toHexString(packet[i] & 0xff)
									.toUpperCase();
					
					bs = bs + " ";

				}
				// Message header must be safed first into the database
				int destadd = 0;
				int lnksrcadd = 0;
				int messlen = 0;
				int grpid = 0;
				int hndlid = 0;

				// ATTENTION: The first byte is 00 and should be ignored
				// destination addess
				destadd |= packet[1] & 0xFF;
				destadd <<= 8;
				destadd |= packet[2] & 0xFF;

				// source address
				lnksrcadd |= packet[3] & 0xFF;
				lnksrcadd <<= 8;
				lnksrcadd |= packet[4] & 0xFF;

				// message length
				messlen |= packet[5] & 0xFF;

				// group id
				grpid |= packet[6] & 0xFF;

				// hndlid
				hndlid |= packet[7] & 0xFF;

				// payload-----------------------------
				// descID
				 int srcId = 0;
				 srcId |= packet[8] & 0xFF;
				 srcId <<= 8;
				 srcId |= packet[9] & 0xFF;
				// counter
				int value = 0;
				value |= packet[12] & 0xFF;
				value <<= 8;
				value |= packet[13] & 0xFF;
				value <<= 8;
				value |= packet[14] & 0xFF;
				value <<= 8;
				value |= packet[15] & 0xFF;
				
				// weight
				int valuew = 0;
				valuew |= packet[16] & 0xFF;
				valuew <<= 8;
				valuew |= packet[17] & 0xFF;
				valuew <<= 8;
				valuew |= packet[18] & 0xFF;
				valuew <<= 8;
				valuew |= packet[19] & 0xFF;
				
				// est
				int est = 0;
				est |= packet[20] & 0xFF;
				est <<= 8;
				est |= packet[21] & 0xFF;
				est <<= 8;
				est |= packet[22] & 0xFF;
				est <<= 8;
				est |= packet[23] & 0xFF;

				try {

					// We send here the values to the database

					db.insertValue(destadd, lnksrcadd, messlen, grpid, hndlid,
							srcId, Float.intBitsToFloat(value), Float.intBitsToFloat(valuew), Float.intBitsToFloat(est));

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (outputOnOff == true) {
					textA.append(bs + " \n");
					textA.selectAll();
				}
				// System.out.flush();
			}
		} catch (IOException e) {
			System.err.println("Error on " + reader.getName() + ": " + e);
		}

	}
}
